﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using ShopDAO;
using ShopModel;
using ShopBLL;

public partial class CategoryMap : BasePage
{
    private int _counter;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Category root = ProductsManager.Instance.GetChildernTreeCategories(-1, true, CommonTools.CurrentLanguageID);

            dlCategoryMap.DataSource = SortCategories(root.Children);
            dlCategoryMap.DataBind();

            List<Producer> producers = ProductsManager.Instance.GetProducersWithCategories(CommonTools.CurrentLanguageID);
            producers.Insert(0, new Producer(Resources.Language.All));
            dlProducers.DataSource = producers;
            dlProducers.DataBind();
        }

    }

    protected List<Category> SortCategories(List<Category> catList)
    {
        catList.Sort(
            delegate(Category c2, Category c1)
            {
                return GetNumberOfSubcategories(c1) - GetNumberOfSubcategories(c2);
            }
            );
        return catList;
    }

    //protected int GetNumberOfSubCategories(Category category)
    //{
    //    int counter = 0;
    //    GetNumberOfSubCategories1(category, ref counter);
    //    return counter;
    //}

    //protected void GetNumberOfSubCategories1(Category category, ref int counter)
    //{
    //    counter = counter + category.Children.Count;
    //    foreach (Category c in category.Children)
    //    {
    //        GetNumberOfSubCategories1(c, ref counter);
    //    }
    //}

    protected int GetNumberOfSubcategories(Category c)
    {
        int result = 0;

        foreach (Category sc in c.Children)
            result += GetNumberOfSubcategories(sc) + 1;

        return result;
    }

    protected void dlCategoryMap_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Category cat = (Category)e.Item.DataItem;

        int margin = 0;
        int fsize = 12;
        int level = 0;
        
        if (cat.Children.Count > 0)
        {
            ListCategories(cat, e, ref margin, ref fsize, ref level);
            level--;
            margin = level * 10;
            if (fsize > 9)
                fsize = 12 - level;
        }
    }

    protected void ListCategories(Category cat, DataListItemEventArgs e, ref int margin, ref int fsize, ref int level)
    {
        HtmlGenericControl cont, div;
        HtmlAnchor a;
        level++;
        margin = level * 10;

        if (fsize > 9)
            fsize = 12 - level;

        cont = (HtmlGenericControl)e.Item.FindControl("cont");

        foreach (Category c in cat.Children)
        {
            a = new HtmlAnchor();
            a.InnerText = c.Name;
            a.HRef = c.IDcategory.ToString() + ",1,1," + Resources.Language.Category.ToLower() +","+ URLParser.ParseUrl(c.Name)+".aspx";

            div = new HtmlGenericControl();
            div.Controls.Add(a);
            div.Style.Add("display", "block");
            div.Style.Add("font-size", fsize + "px");
            div.Style.Add("margin-left", margin + "px");
            cont.Controls.Add(div);

            if (c.Children.Count > 0)
            {
                ListCategories(c, e, ref margin, ref fsize, ref level);
                level--;
                margin = level * 10;
                if (fsize > 9)
                    fsize = 12 - level;
            }

        }
    }

    protected void lbProducer_Load(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        _counter++;
        lb.Font.Bold = false;
        if ((!IsPostBack) && (_counter == 1))
            lb.Font.Bold = true;
    }

    protected void OnCommand(object source, CommandEventArgs e)
    {
        int idProducer = int.Parse(e.CommandArgument.ToString());
        List<Producer> listProducer = ProductsManager.Instance.GetProducersWithCategories(CommonTools.CurrentLanguageID);
        listProducer.Insert(0, new Producer(Resources.Language.All));
        
        Producer SelectedProducer = listProducer.Find(delegate(Producer p) { return p.IDproducer == idProducer; });
        
        LinkButton lb = (LinkButton)source;
        lb.Font.Bold = true;

        Category root = ProductsManager.Instance.GetChildernTreeCategories(-1, true, CommonTools.CurrentLanguageID);

        if (idProducer != -1)
            FiltrateCategories(root, SelectedProducer);

        dlCategoryMap.DataSource = SortCategories(root.Children);
        dlCategoryMap.DataBind();
    }

    protected bool FiltrateCategories(Category c, Producer p)
    {
        bool result = false;
        List<Category> ToDelete = new List<Category>();

        foreach (Category sc in c.Children)
            if (!FiltrateCategories(sc, p))
                ToDelete.Add(sc);
            else
                result = true;

        c.Children.RemoveAll(delegate(Category sc) { return ToDelete.Contains(sc); });

        result = result || p.ProductsInCategories.Contains(c.IDcategory);

        return result;
    }

    //protected bool CheckCategory(Category cat, Producer prod)
    //{

    //    bool exists = false;
    //    for (int j = 0; j < prod.ProductsInCategories.Count; j++)
    //    {
    //        if (cat.IDcategory == prod.ProductsInCategories[j])
    //            exists = true;
    //    }

    //    if (!exists && cat.Children.Count > 0)
    //        foreach (Category c in cat.Children)
    //        {
    //            exists = CheckCategory(c, prod);
    //            if (exists)
    //                return true;

    //        }
    //    return exists;

    //}

    //protected void DeleteCategory(Category category, Producer producer)
    //{

    //    List<Category> ToDelete = new List<Category>();
    //    foreach (Category c in category.Children)
    //    {
    //        if (!CheckCategory(c, producer))
    //            ToDelete.Add(c);
    //        DeleteCategory(c, producer);
    //    }
    //    category.Children.RemoveAll(delegate(Category c1) { return ToDelete.Contains(c1); });
    //}

}
